import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';
import 'package:get/get.dart';

import '../../../../bean/bean_news.dart';
import '../../../../bean/bean_order.dart';

class MyCollectController extends GetxController {
  int totalCount = 0;
  int totalPage = 0;
  int currPage = 0;
  int pageSize = 30;
  List<OrderBean> orderList = [];
  int _tabIndex = 0;
  bool loadingList = false;
  List<NewsBean> _newsList = [];
  bool _isLoading = true;

  List<NewsBean> get newsList => _newsList;

  Future<bool> requestGoodsList() async {
    Map<String, dynamic> map = Map();
    if (_tabIndex == 1) {
      map["collectType"] = "news";
    } else if (_tabIndex == 2) {
      map["collectType"] = "question";
    }
    //
    ResponseInfo responseInfo = await DioUtils.instance
        .getRequest(url: HttpHelper.collectList, queryParameters: map);
    _isLoading = false;
    if (responseInfo.success) {
      _newsList = [];
      List list = responseInfo.data;
      list.forEach((element) {
        NewsBean newsBean = NewsBean.fromJson(element);
        _newsList.add(newsBean);
      });
    }

    update();
    return true;
  }

  ///取消收藏
  Future<bool> cancleCollectFunction(int id) async {
    Map<String, dynamic> map = Map();
    map["newsId"] = id;
    ResponseInfo responseInfo = await DioUtils.instance
        .getRequest(url: HttpHelper.cacleNewsCollect, queryParameters: map);
    _isLoading = false;
    if (responseInfo.success) {
      ToastUtils.showToast("已取消收藏");
      await onPullRefresh();
    }

    update();
    return true;
  }

  Future<bool> onPullRefresh({int index = 0}) async {
    await requestGoodsList();
    return true;
  }

  Future<bool> onRefresh({int index = 0}) async {
    _tabIndex = index;
    await requestGoodsList();
    return true;
  }

  bool get isLoading => _isLoading;
}
